<template>
  <div class="dashboard-container">
    <div class="app-container" :loading="loading">
      <el-card>
        <el-tabs v-model="activeName">
          <el-tab-pane label="角色管理" name="first">
            <el-button
              type="primary"
              style="margin-bottom:20px"
              @click="enterAdd"
            >新增角色</el-button>
            <!-- 表格 -->
            <el-table border="" :data="list">
              <el-table-column
                align="center"
                label="序号"
                type="index"
                width="120"
              />
              <el-table-column
                align="center"
                label="角色名称"
                prop="name"
                width="160"
              />
              <el-table-column align="center" label="描述" prop="description" />
              <el-table-column align="center" label="操作" prop="id">
                <template #default="data">
                  <el-button type="primary">权限分配</el-button>
                  <el-button
                    type="success"
                    @click="enterEdit(data.row.id)"
                  >编辑
                  </el-button>
                  <el-button
                    type="danger"
                    @click="delRole(data.row.id)"
                  >删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <el-row type="flex" justify="center" style="margin-top:20px">
              <el-pagination
                :current-page.sync="page.page"
                :page-size="page.pagesize"
                layout="total, prev, pager, next"
                :total="total"
                @current-change="handleCurrentChange"
              />
            </el-row>
          </el-tab-pane>
          <!-- 公司信息 -->
          <el-tab-pane label="公司信息" name="second">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请递慎修改"
              type="info"
              show-icon
              class="my_alert"
            />
            <!-- 公司表单 -->
            <el-form :model="companyData" label-width="120px">
              <el-form-item style="width:60%" label="公司名称">
                <el-input v-model="companyData.name" disabled />
              </el-form-item>
              <el-form-item style="width:60%" label="公司地址">
                <el-input v-model="companyData.companyAddress" disabled />
              </el-form-item>
              <el-form-item style="width:60%" label="公司邮箱">
                <el-input v-model="companyData.mailbox" disabled />
              </el-form-item>
              <el-form-item style="width:60%" label="备注">
                <el-input v-model="companyData.remarks" disabled />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <!-- 角色修改或新增的对话框 -->
      <el-dialog
        :title="roleData.id ? '编辑角色' : '新增角色'"
        :visible="isVisible"
        :before-close="btnClose"
        @closed="btnClose"
      >
        <el-form ref="roleForm" :model="roleData" @keyup.enter.native="btnOK">
          <el-form-item label-width="120px" label="角色名称">
            <el-input v-model="roleData.name" style="width:80%" />
          </el-form-item>
          <el-form-item label-width="120px" label="角色描述">
            <el-input v-model="roleData.description" style="width:80%" />
          </el-form-item>
        </el-form>
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" @click="btnClose">取消</el-button>
            <el-button
              size="small"
              type="primary"
              @click="btnOK"
            >确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import {
  addRole,
  delRoleById,
  editRole,
  getCompanyInfo,
  getRoleById,
  getRoleList
} from '@/api/setting'
export default {
  computed: {
    ...mapGetters(['companyId'])
  },
  data() {
    return {
      activeName: 'first',
      list: [],
      companyData: {},
      page: { page: 1, pagesize: 3 },
      total: 4,
      isVisible: false,
      roleData: {},
      loading: false
    }
  },
  created() {
    this.getRoleList()
    this.getCompanyInfo()
  },
  methods: {
    async delRole(id) {
      await this.$confirm('确认删除该角色')
      await delRoleById(id)
      this.$message.success('删除角色成功')
      if (this.list.length === 1 && this.page.page > 1) {
        this.page.page--
      }
      this.getRoleList()
    },
    enterAdd() {
      this.isVisible = true
    },
    async enterEdit(id) {
      this.roleData.id = id
      this.roleData = await getRoleById(id)
      this.isVisible = true
    },
    async getCompanyInfo() {
      this.loading = true
      this.companyData = await getCompanyInfo(this.companyId)
      this.loading = false
    },
    async getRoleList() {
      this.loading = true
      const { rows, total } = await getRoleList(this.page)
      this.list = rows
      this.total = total
      this.loading = false
    },
    handleCurrentChange(page) {
      this.page.page = page
      this.getRoleList()
    },
    async btnOK() {
      if (this.roleData.id) {
        await editRole(this.roleData)
      } else {
        await addRole(this.roleData)
      }
      this.$message.success(this.roleData.id ? '编辑角色成功' : '新增角色成功')
      this.isVisible = false
      this.getRoleList()
    },
    btnClose() {
      this.isVisible = false
      this.$refs.roleForm.resetFields()
      this.roleData = {}
    }
  }
}
</script>

<style lang="scss">
.my_alert {
  margin-bottom: 20px;
}
</style>
